//
//  HomeViewController.m
//  HappyToday
//
//  Created by Ale (ziming) Lee on 16/7/26.
//  Copyright © 2016年 ziming_lee. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "HomeModel.h"


@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource,PullTableViewDelegate>{


    UIView *_headerView;
    NSMutableArray *_dataList;

}

@property (weak, nonatomic) IBOutlet PullTableView *homeTableView;

@end

@implementation HomeViewController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {

        self.tabBarItem.image = [UIImage imageNamed:@"icon_tab1_normal@2x"];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self _setCityButton];
    [self _createTableView];
    [self _loadData];

}

-(void)_loadData{

    NSMutableDictionary *rootDataDic = [DataParser jsonObjectFromFileName:@"HomeJSONDirections"];
    NSArray *acticitiesData = [rootDataDic objectForKey:@"data"];
    _dataList = [[NSMutableArray alloc]initWithCapacity:1];
    for (NSDictionary *dic in acticitiesData) {

        HomeModel *model = [[HomeModel alloc]initWithDic:dic];
        [_dataList addObject:model];

    }
    

}
#pragma mark-----CreatTableView------------------------------------
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView{

    NSLog(@"下拉刷新");
}
- (void)pullTableViewDidTriggerLoadMore:(PullTableView*)pullTableView{

    NSLog(@"上啦加载");

}
-(void)_createTableView{

    _homeTableView.delegate = self;
    _homeTableView.dataSource = self;
    _homeTableView.pullDelegate = self;
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 60)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenW, _headerView.height)];
    label.text = @"- 热门推荐 -";
    label.textAlignment = NSTextAlignmentCenter;
    [_headerView addSubview:label];
    _headerView.backgroundColor = [UIColor whiteColor];
    _homeTableView.tableHeaderView = _headerView;


}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _dataList.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"home_cell"];
    if (cell ==nil) {

        cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil] lastObject];

    }
    HomeModel *model = [_dataList objectAtIndex:indexPath.row];
//    cell.pic.image = 
//    cell.title.text = model.title;
//    cell.timestr.text = model.timestr;
//    cell.venuename.text = model.venuename;
//    cell.pricestr.text = model.pricestr;
    cell.homeModel = model;
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 380;

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    _headerView.bottom = _homeTableView.top;

}
#pragma mark-----CreatTableView------------------------------------




#pragma--------mark---------rightBarButtonItems-------------------------
- (IBAction)sreachButton:(UIBarButtonItem *)sender {

    SearchBarButtonItemViewController *searchBarButtonVC = [[SearchBarButtonItemViewController alloc]init];
    [searchBarButtonVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:searchBarButtonVC animated:YES];
}

- (IBAction)messageButton:(UIBarButtonItem *)sender {
    MessageViewController *messageVC = [[MessageViewController alloc]init];
    [messageVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController presentViewController:messageVC animated:YES completion:nil];
}
#pragma--------mark---------rightBarButtonItems-------------------------


#pragma--------mark-------CITY--leftBarButtonItem-------------------------
-(void)_setCityButton{

    UIButton *cityBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    [cityBtn setTitle:@"北京" forState:UIControlStateNormal];
    [cityBtn addTarget:self action:@selector(cityBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:cityBtn];
    





}
-(void)cityBtnAction:(UIButton *)button{

    //1.创建一个CityViewController
    CityViewController *cityVC = [[CityViewController alloc]init];
    [cityVC setHidesBottomBarWhenPushed:YES];
    //2.弹出cityVC
    [self.navigationController presentViewController:cityVC animated:YES completion:nil];
//    [self.navigationController pushViewController:cityVC animated:YES];
    //3.回调cityNameBlock
    [cityVC chuanzhi:^(NSString *cityName) {

        [button setTitle:cityName forState:UIControlStateNormal];
    }];

}
#pragma--------mark-------CITY--leftBarButtonItem-------------------------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
